package com.lagou.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


/**
 * 拦截器 拦截直接跳过登陆进入添加学生页面
 */
public class AddStudentFilter implements Filter {
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		// 将servlet的request和response强转为Httpservlet的request和response，以便于进行session的操作
		HttpServletRequest req = (HttpServletRequest)servletRequest;
		HttpServletResponse resp = (HttpServletResponse)servletResponse;
		// 获取session
		HttpSession session = req.getSession();
		//拿到session中的是否登陆状态值
		Object isLogin = session.getAttribute("isLogin");
		// 如果状态正常，则拦截器放行
		if(isLogin != null && (Boolean)isLogin){
			filterChain.doFilter(req,resp);
		} else {
			// 否则重定向到登陆页面
			resp.sendRedirect("/student_manage/login.html");
		}
	}

	@Override
	public void destroy() {

	}
}
